<?php
/**
 * qiniu oss
 *
 */
defined('InShopNC') or exit('Access Invalid!');
require_once BASE_DATA_PATH.'/api/qiniu/autoload.php';

final class oss {
    private static $access_key;
    private static $secret_key;
    private static $bucket;
    private static $auth;

    private static function _init(){
        self::$access_key = C('oss.access_key');
        self::$secret_key = C('oss.secret_key');
        self::$bucket = C('oss.bucket');
        // 构建鉴权对象
        self::$auth = new Qiniu\Auth(self::$access_key, self::$secret_key);
    }

    //上传
    public static function upload($src_file,$new_file){
        self::_init();
        try{
            // 生成上传 Token
            $token = self::$auth->uploadToken(self::$bucket);
            // 初始化 UploadManager 对象并进行文件的上传。
            $uploadMgr = new Qiniu\Storage\UploadManager();
            // 调用 UploadManager 的 putFile 方法进行文件的上传。
            list($ret, $err) = $uploadMgr->putFile($token, $new_file, $src_file);
            if ($err) {
                return false;
            } else {
                return true;
            }
        } catch (Exception $ex){
            return false;
        }
    }

    //删除
    public static function del($img_list = array()) {
        self::_init();
        try{
            $config = new Qiniu\Config();
            $bucketManager = new Qiniu\Storage\BucketManager(self::$auth, $config);
            $ops = $bucketManager->buildBatchDelete(self::$bucket, $img_list);
            list($ret, $err) = $bucketManager->batch($ops);
            if ($err) {
                return false;
            } else {
                return true;
            }
        } catch (Exception $ex){
            return false;
        }
    }
}